/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.vouchersend;
import shop.core.common.bean.JSONResult;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;
import shop.services.manage.voucherhave.VoucherhaveService;
import shop.services.manage.voucherhave.bean.Voucherhave;
import shop.services.manage.vouchersend.VouchersendService;
import shop.services.manage.vouchersend.bean.Vouchersend;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**   
 * @类名称：VouchersendAction      
 * @创建人：wzl
 * @创建时间：2018-01-03 下午17:06:56      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/vouchersend/")
public class VouchersendAction extends BaseController<Vouchersend> {
    private static final Logger logger = LoggerFactory.getLogger(VouchersendAction.class);
    @Autowired
    private VouchersendService vouchersendService;
    @Autowired
    private VoucherhaveService voucherhaveService;
    @Autowired
    private UserService userService;
    private static final String page_toList = "/manage/vouchersend/vouchersendList";
    public VouchersendService getService() {
        return vouchersendService;
    }

    private VouchersendAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 派送礼卷
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "sendVoucher", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult sendVoucher(Vouchersend vouchersend) throws Exception {
        JSONResult jsonResult = new JSONResult();
        vouchersendService.insert(vouchersend);
        User user=new User();
        user.setRid(vouchersend.getRoleID());
        List<User> userList=userService.selectList(user);
        if(userList!=null&&userList.size()>0){
            for(User userInfo :userList){
                Voucherhave voucherhave=new Voucherhave();
                voucherhave.setUserID(userInfo.getId());
                voucherhave.setVoucherID(vouchersend.getVoucherID());
                Voucherhave voucherhaveInfo=voucherhaveService.selectOne(voucherhave);
                if(voucherhaveInfo==null){
                    voucherhaveService.insert(voucherhave);
                }else{
                    voucherhaveInfo.setAmount(voucherhaveInfo.getAmount()+1);
                    voucherhaveService.update(voucherhaveInfo);
                }
            }
        }
        return jsonResult;
    }
}
